package com.jweb.framework.http.support;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.MessageSourceResolvable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.jweb.framework.http.ViewNameResolver;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：默认视图名称解析
 */

public class DefaultViewNameResolver implements ViewNameResolver {
	
	/*
	 * responseSuffix 在之前加此值为后缀，拼接视图名称。
	 */
	private String responseSuffix ;
	/*
	 * useLocale 是否使用locale拼接视图名称。
	 */
	private boolean useLocale = true ;
	/*
	 * viewNames 设置交易需要Forward或Redirect的properties文件路径
	 */
	private Properties viewNames ;
	/*
	 * defaultErrorView 默认的错误视图路径
	 */
	private String defaultErrorView = "/error.jsp";
	/*
	 * errorViewNames 设置交易抛出异常的properties文件路径
	 */
	private Properties errorViewNames ;
	
	/**
	 * 默认页面的后缀
	 */
	private String defaultViewSuffix = ".jsp" ;
	
	public void setResponseSuffix(String str){
		this.responseSuffix = str ;
	}
	
	public void setUseLocale(boolean flag){
		this.useLocale = flag ;
	}
	
	public void setViewNames(Properties pros){
		this.viewNames = pros ;
	}
	
	public void setErrorViewNames(Properties pros){
		this.errorViewNames = pros ;
	}
	
	public void setDefaultErrorView(String str){
		this.defaultErrorView = str ;
	}
	
	public void setDefaultViewSuffix(String suffix){
		this.defaultViewSuffix = suffix ;
	}

	public String getViewName(HttpServletRequest request , Map<String, Object> map , String str , Throwable throwable) {
		
		String str1 = null ;
		String str2 = null ;
		Object obj = null ;
		
		if(throwable == null){
			int i = 0 ;
			if(this.viewNames != null){ // true 
				str2 = Utils.getIdentity(request); 
				if(str!=null && !str.equals("")){ 
					StringBuffer sb = new StringBuffer(str2);
					sb.append("_").append(str);
					str1 = this.viewNames.getProperty(sb.toString());
					if(str1 != null){
						i = 1 ;
					}
				}
				
				if(str1 == null){
					str1 = this.viewNames.getProperty(str2); 
				}
				if(str1!=null && (str1.indexOf(':')!=-1)){
					return str1 ;
				}
			}
			
			if(str1 == null){
				str1 = Utils.getPathInServlet(request); 
			}
			if(i==0){
				str1 = invokeName(str1 , str); 
			}
		}
		else {
			
			if(this.errorViewNames != null){
				obj = throwable.getClass().getName();
				if(throwable instanceof MessageSourceResolvable){
					str2 = ((MessageSourceResolvable)throwable).getCodes()[0];
					str1 = this.errorViewNames.getProperty(obj+"."+str2);
					if(str1 == null){
						str1 = this.errorViewNames.getProperty(str2);
					}
					if(str1 == null){
						str1 = this.errorViewNames.getProperty((String)obj);
					}
				}
				else {
					str1 = this.errorViewNames.getProperty((String)obj);
				}
				
				if(str1!=null && ("$".equals(str1))){
					str1 = Utils.getRefererResolver(request).resolveReferer(request, null);
				}
			}
			
			if(str1 == null){
				str1 = this.defaultErrorView ;
			}
		}
		
		if(!str1.startsWith("/")){
			str1 = "/" + str1 ;
		}
		
		if(!this.useLocale){
			return str1 ;
		}
		
		Object object = RequestContextUtils.getLocale(request);
		str2 = "/" + object + str1 ;
		
		return str2 ; 
	}

	private String invokeName(String str1, String str2) {
		
		int i = str1.lastIndexOf('.');
		
		String str11 = (i<0)? str1 : str1.substring(0 , i); 
		String str22 = (i<0)? "" : str1.substring(i); 
		
		if(StringUtils.hasText(this.responseSuffix)){
			str11 = str11 + this.responseSuffix ;
		}
		if(StringUtils.hasText(str2)){
			str11 = str11 +"_" + str2 ;
		}
		if(str22.length() == 0 || (".do".equals(str22))){
			//str22 = ".jsp";
			str22 = this.defaultViewSuffix ;
		}
		str1 = str11 + str22 ;
		
		return str1 ; 
	}

}
